package site.user.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import modules.recruit.entity.ApplyManager;
import modules.recruit.form.ApplyManagerForm;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.guinea.core.service.EntityService;

import ex.recruit.service.ApplyManagerServiceEx;

public class ApplyManagerActionSite extends ApplyCommonStep {
	private ApplyManagerServiceEx applyManagerServiceEx;

	protected String position;

	public EntityService getEntityService() {
		return applyManagerServiceEx;
	}

	public ApplyManagerServiceEx getApplyManagerServiceEx() {
		return applyManagerServiceEx;
	}

	public void setApplyManagerServiceEx(
			ApplyManagerServiceEx applyManagerServiceEx) {
		this.applyManagerServiceEx = applyManagerServiceEx;
	}
	
	/**
	 * 初始化业务对象的函数.
	 */
	protected void findEntity(ActionForm form, HttpServletRequest request) {
		super.findEntity(form,request);
		position = request.getParameter("position");
		if(entity == null && applyBase != null && position != null){
			entity = getApplyManagerServiceEx().getManagerByBasePosition(
					applyBaseId, position);
		}
		//不是由ID参数得到的entity
		if (id == null && entity != null) {
			ApplyManager applyManager = (ApplyManager)entity;
			ApplyManagerForm applyManagerForm = (ApplyManagerForm) form;
			applyManagerForm.setId(applyManager.getId());
			id = applyManager.getId();
		}
	}

	/**
	 * 显示修改对象Form的Action函数.
	 */
	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		if (entity == null)
			entity = getApplyManagerServiceEx().getManagerByBasePosition(
					applyBaseId, position);

		bindForm(form, entity);
		ApplyManagerForm applyManagerForm = (ApplyManagerForm)form;
		applyManagerForm.setApplyBaseId(applyBaseId);
		
		return handleEdit(mapping,form,request,response);
	}
	
	protected void doInitEntity(HttpServletRequest request, ActionForm form) {
		super.doInitEntity(request, form);
		if(isCreateOperation){
			ApplyManager applyManager = (ApplyManager) entity;
			applyManager.setBase(applyBase);
			entity = applyManager;
		}
	}

	/**
	 * 保存对象的Action函数.
	 */
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		if (!validateForm(mapping, form, request))
			return handleFormErrors(mapping, form, request, response);
		
		doInitEntity(request, form);

		doSaveEntity(request, form);

		String direction = request.getParameter("direction");
		if (direction != null && direction.equals("next"))
			return new ActionForward(
					"/site/user/applyPlan.do?method=edit&applyBaseId="
							+ applyBaseId.toString(), true);
		else if (direction != null && direction.equals("last"))
			return new ActionForward(
					"/site/user/applyBg.do?method=edit&applyBaseId="
							+ applyBaseId.toString(), true);
		else {
			saveMessage(request, "entity.saved");
			ApplyManagerForm applyManagerForm = (ApplyManagerForm) form;
			applyManagerForm.setId((Long) id);
			return handleEdit(mapping,form,request,response);
		}
	}
}
